//
//  DBHead.m
//  Pods
//
//  Created by arons on 16/9/19.
//
//

#import "DBHead.h"
#import "DBDatabaseConn.h"


@implementation DBHead

+ (NSString *)tableName {
    return @"t_head_datas";
}

+ (NSString *)sqlSnipet {
    return @"headID INTEGER, headURL TEXT, latestUseTime INTEGER, offPoint TEXT, headType INTEGER";
}

/**
 *  创建表
 */
+ (void)createTableIfNotExists {
    NSString* sql = @"CREATE TABLE IF NOT EXISTS t_head_datas (headID INTEGER, headURL TEXT, latestUseTime INTEGER, offPoint TEXT, headType INTEGER, PRIMARY KEY (headID));";
    
    [[DBDatabaseConn sharedDBDatabaseConn].databaseQueue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:sql];
    }];
}

+ (BOOL)updateDatasWithSql:(NSString*)sql {
    BOOL __block result = NO;
    [[DBDatabaseConn sharedDBDatabaseConn].databaseQueue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:sql];
    }];
    return result;
}

+ (NSArray*)quertDatasWithSql:(NSString*)sql {
    NSMutableArray *results = [NSMutableArray array];
    // 执行SQL
    [[DBDatabaseConn sharedDBDatabaseConn].databaseQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *set = [db executeQuery:sql];
        
        while (set.next) {
            
            DBHead* data = [DBHead new];
            
            int headID = [set intForColumn:@"headID"];
            data.headID = headID;
            
            NSString* headURL = [set objectForColumnName:@"headURL"];
            data.headURL = [NSURL URLWithString:headURL];
            
            NSTimeInterval latestUseTime = [set doubleForColumn:@"latestUseTime"];
            data.latestUseTime = latestUseTime;
            
            NSString* offPointStr = [set objectForColumnName:@"offPoint"];
            if (offPointStr != nil
                && [offPointStr isKindOfClass:[NSString class]]
                && offPointStr.length > 0) {
                NSArray* offPointArr = [offPointStr componentsSeparatedByString:@","];
                if (offPointArr.count == 2) {
                    CGFloat x = [offPointArr[0] floatValue];
                    CGFloat y = [offPointArr[1] floatValue];
                    data.offPoint = CGPointMake(x, y);
                }
            }
            
            [results addObject:data];
        }
    }];
    return results;
}

/**
 *  查询
 */
+ (NSArray*)datasWithParams:(NSDictionary*)params {
    // 根据请求参数生成对应的查询SQL语句
    NSMutableString* sql = [NSMutableString stringWithString:@"SELECT * FROM t_head_datas WHERE 1=1"];
    [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[NSString class]]) {
            [sql appendString:[NSString stringWithFormat:@" AND %@='%@'", key, obj]];
        }else {
            [sql appendString:[NSString stringWithFormat:@" AND %@=%@", key, obj]];
        }
    }];
    
    return [self quertDatasWithSql:sql];
}

+ (void)saveData:(DBHead*)head {
    if (head == nil || head.headURL == nil) {
        return;
    }
    [[DBDatabaseConn sharedDBDatabaseConn].databaseQueue inDatabase:^(FMDatabase *db) {

        NSString* offPointStr = [NSString stringWithFormat:@"%@,%@", @(head.offPoint.x), @(head.offPoint.y)];
        BOOL result = [db executeUpdateWithFormat:@"REPLACE INTO t_head_datas(headID, headURL, latestUseTime, offPoint) VALUES (%d, %@, %f, %@);", (head.headID),
                       ValueOrEmpty(head.headURL.absoluteString),
                       head.latestUseTime,
                       offPointStr];
        NSLog(@"DBHead save result: %d", result);
    }];
}

/**
 *  删除一个头像
 *
 *  @param cateId 头像ID
 */
+ (void)deleteHeadWithHeadId:(NSInteger)cateId {
    NSString* sql = [NSString stringWithFormat:@"DELETE FROM t_head_datas WHERE 1=1 AND headID = %d", (int)cateId];
    [self updateDatasWithSql:sql];
}

@end
